package com.nuanshui.heatedloan.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.annotation.web.servlet.configuration.EnableWebMvcSecurity;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.web.authentication.RememberMeServices;
import org.springframework.security.web.authentication.rememberme.JdbcTokenRepositoryImplEx;
import org.springframework.security.web.authentication.rememberme.PersistentTokenBasedRememberMeServicesEx;
import org.springframework.security.web.authentication.rememberme.PersistentTokenRepository;

import javax.sql.DataSource;



@Configuration
@EnableWebMvcSecurity
@ImportResource("classpath:META-INF/spring-security-context.xml")
class SecurityConfig extends WebSecurityConfigurerAdapter {

	@Autowired()
	DataSource common_dataSource;


	@Autowired
	private UserDetailsService userDetailService;

	@Value("${rememberMeServices.key}")
	String remMeKey;

	@Value("${rememberMeServices.token.calidity.seconds}")
	int validSeconds;

	@Bean
	public PersistentTokenRepository persistentTokenRepository() {
		return new JdbcTokenRepositoryImplEx(common_dataSource);
	}

	@Bean
	public RememberMeServices rememberMeServices() {
		PersistentTokenBasedRememberMeServicesEx svc = new PersistentTokenBasedRememberMeServicesEx(remMeKey,
				userDetailService, persistentTokenRepository());
	    // TokenBasedRememberMeServices svc = new TokenBasedRememberMeServices(remMeKey, userService);
		svc.setAlwaysRemember(true);
		svc.setTokenValiditySeconds(validSeconds);
		svc.setCookieName("lbAuthToken");
		return svc;
	}
}